package com.ja.openchat.ui.me;

import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentSetPwdBinding;
import com.ja.openchat.viewmodels.SetPwdViewModel;
import com.zy.openchat.util.KeyboardHelper;

import me.goldze.mvvmhabit.base.BaseFragment;

//设置密码
public class SetPwdFragment extends BaseFragment<FragmentSetPwdBinding, SetPwdViewModel> {
    private Bundle arguments;

    @Override
    public void initParam() {
        arguments = getArguments();
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.setData(arguments);
        if (binding.etPwd.getVisibility() == View.VISIBLE) {
            openKeyBord(binding.etPwd);
        } else {
            openKeyBord(binding.etPwd1);
        }
    }

    void openKeyBord(EditText view) {
        view.postDelayed(() -> {
            try {
                view.requestFocus();
                KeyboardHelper.openKeyBoard(getContext(), view);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, 500);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_set_pwd;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.pSwitchEvent.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                if (viewModel.uc.pSwitchEvent.getValue()) {
                    //密码可见
                    //在xml中定义id后,使用binding可以直接拿到这个view的引用,不再需要findViewById去找控件了
                    binding.ivSwichPasswrod.setImageResource(R.mipmap.pwd_openeye);
                    binding.etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //密码不可见
                    binding.ivSwichPasswrod.setImageResource(R.mipmap.pwd_closeeye);
                    binding.etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        viewModel.uc.p1SwitchEvent.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                if (viewModel.uc.p1SwitchEvent.getValue()) {
                    //密码可见
                    //在xml中定义id后,使用binding可以直接拿到这个view的引用,不再需要findViewById去找控件了
                    binding.ivSwichPasswrod1.setImageResource(R.mipmap.pwd_openeye);
                    binding.etPwd1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //密码不可见
                    binding.ivSwichPasswrod1.setImageResource(R.mipmap.pwd_closeeye);
                    binding.etPwd1.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        viewModel.uc.p2SwitchEvent.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                if (viewModel.uc.p2SwitchEvent.getValue()) {
                    //密码可见
                    //在xml中定义id后,使用binding可以直接拿到这个view的引用,不再需要findViewById去找控件了
                    binding.ivSwichPasswrod2.setImageResource(R.mipmap.pwd_openeye);
                    binding.etPwd2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //密码不可见
                    binding.ivSwichPasswrod2.setImageResource(R.mipmap.pwd_closeeye);
                    binding.etPwd2.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
    }
}